﻿using UnityEngine;
using System.Collections;

public class ShopWdw : MonoBehaviour {

    public TRUCK_EVENT EventID;
	public int WdwNum = 0; // 1 - Hire/ 2 - UNIT / 3 - BUILD / 4 - STAT
	
	bool UpdateOnce = true;
	bool IN_POS_BeforeSwipe;
	
	void Start(){
        Initializing();

        GetComponent<BoxCollider>().center = new Vector3(-47.81f, -21.7f);
        GetComponent<BoxCollider>().size = new Vector3(3.5f, 22.1f);

        DistinguishEvent();

        if (GameObject.Find("BuildUI").transform.childCount != 0)
        {
            for (int i = 0; i < GameObject.Find("BuildUI").transform.childCount; ++i)
            {
                Destroy(GameObject.Find("BuildUI").transform.GetChild(i).gameObject);
                Destroy(GameObject.Find("Price(Clone)").gameObject);
            }
        }
	}

    public void Initializing()
    {
        IN_POS_BeforeSwipe = true;
        UpdateOnce = true;

        gameObject.GetComponent<TweenPosition>().Play(true);
        transform.parent.GetComponent<UIPanel>().depth = 1;
        transform.parent.parent.GetComponent<RightWdwMgr>().CurWinNum = WdwNum;

        GameObject.Find("MainCamera").GetComponent<AudioSource>().PlayOneShot(Resources.Load<AudioClip>("Audio/Drawer_Open"));
    }

    void DistinguishEvent() // 이벤트 구분하여 하위클래스 생성
    {
        switch(EventID)
        {
            case TRUCK_EVENT.REINFORCE:
                gameObject.AddComponent<HireWindow>();
                break;

            case TRUCK_EVENT.AMMO:
                (Instantiate(Resources.Load("Prefaps/UI/Paper/AmmoBills/AmmoBills") as GameObject) as GameObject).transform.parent = transform;
                break;

            case TRUCK_EVENT.SUPPLY:
                (Instantiate(Resources.Load("Prefaps/UI/Paper/Supply/SupplyBills") as GameObject) as GameObject).transform.parent = transform;
                break;
        }
        transform.GetChild(1).localPosition = new Vector3(3, 3.15f);
        transform.GetChild(1).localScale = new Vector3(0.65f, 0.99f);
        transform.GetChild(1).GetComponent<UISprite>().depth = GetComponent<UISprite>().depth + 1; // 이벤트 이름 일일히 따져가며 depth조절보단 한번에 묶어서 depth관리
    }
	
	void OnClick(){
		transform.parent.parent.GetComponent<RightWdwMgr>().CurWinNum = 0;

        GameObject.Find("MainCamera").GetComponent<AudioSource>().PlayOneShot(Resources.Load<AudioClip>("Audio/Drawer_Close"));
	}
	
	public void DelWdw(){
        gameObject.SetActive(false);
        GameObject.Find("RightBtn").transform.GetChild(3).gameObject.SetActive(true);
        GameObject.Find("ShopBtn(Clone)").GetComponent<ShopBtn>().WdwClose();
	}
	
	public void InPos(){
		if(IN_POS_BeforeSwipe){
			gameObject.GetComponent<TweenPosition> ().from = new Vector3 ( -89.0f, 0.0f );
			gameObject.GetComponent<TweenPosition> ().to = Vector3.zero;
		}
	}
	
	public void SwipeToRightWdw(){
		transform.parent.GetComponent<UIPanel> ().depth = 0;
		
		if (gameObject.transform.localPosition.x == -89.0f) {
			gameObject.GetComponent<TweenPosition> ().enabled = true;
			gameObject.GetComponent<TweenPosition> ().from = gameObject.transform.localPosition;
			gameObject.GetComponent<TweenPosition> ().to = Vector3.zero;
			gameObject.GetComponent<TweenPosition> ().ResetToBeginning ();
			IN_POS_BeforeSwipe = true;
			
		} else if (gameObject.transform.localPosition.x != -89.0f) { // 다안도착햇을때
			gameObject.GetComponent<TweenPosition> ().Play(false);
			IN_POS_BeforeSwipe = false;
		}
		
		gameObject.GetComponent<TweenPosition> ().RemoveOnFinished(new EventDelegate(DelWdw));
		EventDelegate.Add (gameObject.GetComponent<TweenPosition> ().onFinished, DelWdw);
	}
	
	void OnDisable(){
		
		gameObject.GetComponent<TweenPosition> ().enabled = true;
		
		if (IN_POS_BeforeSwipe) {
			gameObject.GetComponent<TweenPosition> ().ResetToBeginning();
			
			gameObject.GetComponent<TweenPosition> ().from = Vector3.zero;
			gameObject.GetComponent<TweenPosition> ().to = new Vector3 ( -89.0f, 0.0f );
			
		} else {
		}
		
		gameObject.GetComponent<TweenPosition> ().RemoveOnFinished(new EventDelegate(DelWdw));
		gameObject.transform.localPosition = Vector3.zero;
		
		
	}
	
	void Update(){
		if ( UpdateOnce && transform.parent.parent.GetComponent<RightWdwMgr> ().CurWinNum != WdwNum) {
			SwipeToRightWdw();
			UpdateOnce = false;
		}

        if (Input.GetKeyUp(KeyCode.Escape))
        {
            OnClick();
        }
	}

	public void DelThis(){
        transform.parent.parent.GetComponent<RightWdwMgr>().CurWinNum = 0;
		gameObject.GetComponent<TweenPosition> ().from.x = 30;
        Destroy(gameObject);
	}
}
